package CGW.service;

import CGW.bean.Food;
import CGW.bean.Page;
import CGW.dao.FoodDao;

import java.util.List;

public class FoodService {
    private FoodDao dao;

    public FoodService(FoodDao dao) {
        this.dao = dao;
    }

    public List<Food> queryAll(){
        List<Food> ret = dao.all(1, 100);
        dao.close();
        return ret;
    }

    public Page<Food> queryAll(int page, int pageSize){
        int total = dao.total();
        List<Food> ret = dao.all(page, pageSize);
        Page<Food> pageBean = new Page<>(ret, page, (int)Math.ceil(total*1.0/pageSize), pageSize);
        dao.close();
        return pageBean;
    }

    public Food get(int user_id){
       return dao.get(user_id);
    }

    public boolean insert(Food food){
        int rows = dao.add(food);
        dao.commit();
        dao.close();
        return rows > 0;
    }

    public boolean update(Food food){
        int rows = dao.update(food);
        dao.commit();
        dao.close();
        return  rows > 0;
    }
    public boolean delete(int food_id){
        int rows = dao.delete(food_id);
        dao.commit();
        dao.close();
        return  rows > 0;
    }


    public void close(){
        dao.close();
    }
}
